/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.axiom.utility;

import com.axiom.persistence.instructor_detail_persist;
import com.axiom.vo.course_detail;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.html.simpleparser.CellWrapper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import org.hibernate.ScrollableResults;

/**
 *
 * @author Shuhail
 */
public class PDFGenerator {
     private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
      Font.BOLD);
    
    public static PDFGenerator getInstance() {
        
        return new PDFGenerator();
    }
    
    public void generatePDF(String path , ScrollableResults res ,int noOfRows,String name) {
        
        try {
            
            OutputStream os = new FileOutputStream(new File(path));
            
            Document doc = new Document();
            PdfWriter.getInstance(doc, os);
            
            doc.open();
            addTitlePage(doc, name);
           // doc.add(new Paragraph("                             "+name));
            //doc.add(new LineSeparator());
            //oc.add(new Paragraph("\n\n\n"));
            int j =1;
            while(res.next()) {
                StringBuffer sb = new StringBuffer();
                
                sb.append(j+". ");
                for(int i=0;i<noOfRows;i++) {
                    
                    sb.append(res.get(i));
                    sb.append("                  ");
                }
                doc.add(new Paragraph(sb.toString()));
                
                System.out.println(sb.toString());
                j++;
            }
            doc.close();
            
        }
        catch(Exception e) {
            
        }
    }
    
     public void generatePDFForCourse(String path , List res ,int noOfRows,String name) {
        
        try {
            
            OutputStream os = new FileOutputStream(new File(path));
            
            Document doc = new Document();
            PdfWriter.getInstance(doc, os);
            
            doc.open();
            addTitlePage(doc, name);
            Iterator it = res.iterator();
            while(it.hasNext()) {
                StringBuffer sb = new StringBuffer();
                course_detail detail = (course_detail)it.next();
                sb.append(detail.getName()+"       "+detail.getFees());
                doc.add(new Paragraph(sb.toString()));
                System.out.println(sb.toString());
            }
            doc.close();
            
        }
        catch(Exception e) {
            
        }
    }
     
     private static void addTitlePage(Document document, String title)
      throws DocumentException {
    Paragraph preface = new Paragraph();
    // We add one empty line
    addEmptyLine(preface, 1);
    // Lets write a big header
    preface.add(new Paragraph(title, catFont));

    addEmptyLine(preface, 1);
    // Will create: Report generated by: _name, _date
   

    document.add(preface);
    // Start a new page
    //document.newPage();
  }
      private static void addEmptyLine(Paragraph paragraph, int number) {
    for (int i = 0; i < number; i++) {
      paragraph.add(new Paragraph(" "));
    }
  }

    
    public static void main(String[] args) {
        
        PDFGenerator pdf = new PDFGenerator();
        
        pdf.generatePDF("C:\\Users\\Shuhail\\Desktop\\Test1.pdf",instructor_detail_persist.getInstance().getIntructorDetailAndCourse() , 3 ,"Student Details");
    }
}
